package com.finance.util;


import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Http请求配置
 */
public class HttpConfig {

    // 编码
    private String charset;
    // 是否保持cookie
    private boolean storeCookie = true;
    // 代理IP
    private String proxyHost;
    // 代理端口
    private int proxyPort;
    private int socketTimeout = 5 * 60 * 1000;
    private int connectTimeout = 2 * 60 * 1000;
    // 最大允许redirect次数
    private int maxRedirects = 5;
    // 最大重试次数
    private int retrys = 3;

    private Map<String, String> header = new LinkedHashMap<>();

    private static final String[] agents = {
            // ie
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)",
            // Opera
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
            // Firefox
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
            // Safari
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
            // Chrome
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
            // 360
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
    };

    public static HttpConfig spider() {
        HttpConfig config = new HttpConfig();
        config.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        config.addHeader("User-Agent", agents[(int) (System.nanoTime() % agents.length)]);
        config.addHeader("Connection", "keep-alive");
        config.addHeader("Accept-Encoding", "gzip, deflate");
        config.addHeader("Accept-Language:", "zh-CN,zh;q=0.9");
        config.addHeader("Cache-Control", "max-age=0");

        return config;
    }


    public static HttpConfig chrome() {
        HttpConfig config = new HttpConfig();
        config.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        config.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36");
        config.addHeader("Connection", "keep-alive");
        config.addHeader("Accept-Encoding", "gzip, deflate");
        config.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
        //    config.addHeader("Cache-Control", "max-age=0");
        config.addHeader("Upgrade-Insecure-Requests", "1");

        return config;
    }


    public static HttpConfig firefox() {
        HttpConfig config = new HttpConfig();
        config.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0");
        config.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8");
        config.addHeader("Connection", "keep-alive");
        config.addHeader("Accept-Encoding", "gzip, deflate");
        config.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        //    config.addHeader("Cache-Control", "max-age=0");
        config.addHeader("Upgrade-Insecure-Requests", "1");

        return config;
    }

    public int getRetrys() {
        return retrys;
    }

    public HttpConfig setRetrys(int retrys) {
        this.retrys = retrys;
        return this;
    }

    public int getSocketTimeout() {
        return socketTimeout;
    }

    public HttpConfig setSocketTimeout(int socketTimeout) {
        this.socketTimeout = socketTimeout;
        return this;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public HttpConfig setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public int getMaxRedirects() {
        return maxRedirects;
    }

    public HttpConfig setMaxRedirects(int maxRedirects) {
        this.maxRedirects = maxRedirects;
        return this;
    }

    public HttpConfig addHeader(String key, String value) {
        header.put(key, value);
        return this;
    }

    public boolean isStoreCookie() {
        return storeCookie;
    }

    public HttpConfig setStoreCookie(boolean storeCookie) {
        this.storeCookie = storeCookie;
        return this;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public HttpConfig setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
        return this;
    }

    public int getProxyPort() {
        return proxyPort;
    }

    public HttpConfig setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
        return this;
    }

    public HttpConfig setProxy(String host, Integer port) {
        this.proxyHost = host;
        this.proxyPort = port;
        return this;
    }

    public Map<String, String> getHeader() {
        return header;
    }

    public HttpConfig setHeader(Map<String, String> header) {
        this.header = header;
        return this;
    }

    public String getCharset() {
        return charset;
    }

    public HttpConfig setCharset(String charset) {
        this.charset = charset;
        return this;
    }
}
